$(function () {
    $("#jqGrid").Grid({
        url: '../toolsrecord/list',
        colModel: [
            {label: '校对记录', name: 'proofreading', index: 'PROOFREADING', width: 80},
            {label: '序号', name: 'serialnumber', index: 'SERIALNUMBER', width: 80},
            {label: '名称', name: 'name', index: 'NAME', width: 80},
            {label: '图号', name: 'mapNo', index: 'MAPNO', width: 80},
            {label: '型号规格', name: 'spec', index: 'SPEC', width: 80},
            {label: '适应机型', name: 'adapDev', index: 'ADPDEV', width: 80},
            {label: '一次使用量', name: 'onceUse', index: 'ONCEUSE', width: 80},
            {label: '工装类别', name: 'frocktype', index: 'FROCKTYPE', width: 80},
            {label: '生产线代码', name: 'prodCode', index: 'PRODCODE', width: 80},
            {label: '生产线名称', name: 'prodName', index: 'PRODNAME', width: 80},
            {label: '使用工序', name: 'proCode', index: 'PROCODE', width: 80},
            {label: '工序使用名称', name: 'proName', index: 'PRONAME', width: 80},
            {label: '管控方式', name: 'managerType', index: 'MANAGERTYPE', width: 80},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '备注', name: 'remark', index: 'REMARK', width: 80}]
    });
});

var data1;
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        showTree : true,
        title: null,
        toolsRecord: {
            managerType : '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ],
            spec: [
                {required: true, message: '型号规格不能为空', trigger: 'blur'}
            ],
            adapDev: [
                {required: true, message: '适应机型不能为空', trigger: 'blur'}
            ],
            frocktype: [
                {required: true, message: '工装类别不能为空'}
            ],
            prodCode: [
                {required: true, message: '生产线代码不能为空', trigger: 'blur'}
            ],
            prodName: [
                {required: true, message: '生产线名称不能为空', trigger: 'blur'}
            ],
            proCode: [
                {required: true, message: '使用工序不能为空', trigger: 'blur'}
            ],
            proName: [
                {required: true, message: '工序使用名称不能为空', trigger: 'blur'}
            ],
            managerTypeName: [
                {required: true, message: '管控方式不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: '', //姓名
            vague_search: '', //模糊搜索
            mapNo: '', //图号
            spec: '', //规格型号
        },
        data1: [
            {
                title: 'NE1一期新基地',
                expand: true,
                children: [
                    {
                        title: '周期性精密校对易损件',
                        expand: true,
                    },
                    {
                        title: '铸造',
                        expand: true,
                        children: [
                            {
                                title: '铸造一',
                                key: '1',
                            },
                            {
                                title: '铸造二'
                            },
                            {
                                title: '铸造三'
                            }
                        ]
                    },
                    {
                        title: '机加',
                        expand: true,
                        children: [
                            {
                                title: '机加一'
                            },
                            {
                                title: '机加二'
                            },
                            {
                                title: '机加三'
                            }
                        ]
                    },
                    {
                        title: '机总',
                        expand: true,
                        children: [
                            {
                                title: '机总一'
                            },
                            {
                                title: '机总二'
                            },
                            {
                                title: '机总三'
                            }
                        ]
                    }, {
                        title: 'H系列五期',
                        expand: true,
                    }
                ]
            }
        ],
        treeDataList : [],
        frocktypeList: enumsToList('WORK_CLOTHES_TYPE'),
    },
    methods: {
        managerClick: function () {
            var _this = this;
            openWindow({
                title : '管控方式',
                area: ['300px', '450px'],
                content: jQuery("#treeDatas"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = data1.getSelectedNodes();
                    vm.toolsRecord.managerTypeName = _this.treeDataList[0].title;
                    vm.toolsRecord.managerType = _this.treeDataList[0].nodeKey;
                    layer.close(index);
                }
            })
        },
        treeCleans : function(index){
            if (index.length > 0){
                this.treeDataList = index;
            }
        },
        treeClean: function (index) {
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.toolsRecord = {};
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.toolsRecord.id == (null || '') ? "../toolsrecord/save" : "../toolsrecord/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.toolsRecord),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../toolsrecord/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../toolsrecord/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.toolsRecord = r.toolsRecord;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: this.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    },created: function (){
        data1 = this.data1;
    }
});